from PySide6.QtWidgets import QFrame, QStackedWidget, QHBoxLayout
from qfluentwidgets import FlowLayout
from resource import OfficeIcon
from components import AppCard
from .excel_view import RandomExcelFrame, MergeExcelView, ExcelToPdfView, SheetToExcelView, UnionQueryView


class ExcelHomeFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = FlowLayout(self, needAni=True)

        self.randomExcel = AppCard(OfficeIcon.DATA1, 'Random Excel', '批量生成随机数据，并保存为Excel')
        self.to_pdf = AppCard(OfficeIcon.PDF4, 'Excel To PDF', 'Excel转PDF')
        self.merge_excel = AppCard(OfficeIcon.EXCEL3, 'Merge Excel', 'Excel合成')
        self.sheet_excel = AppCard(OfficeIcon.EXCEL4, 'Sheet To Excel', '根据Sheet页拆分Excel')
        self.key_merge_excel = AppCard(OfficeIcon.EXCEL, 'Union To Excel', '根据Key合并Excel')

        self.layout.addWidget(self.randomExcel)
        self.layout.addWidget(self.to_pdf)
        self.layout.addWidget(self.merge_excel)
        self.layout.addWidget(self.sheet_excel)
        self.layout.addWidget(self.key_merge_excel)


class ExcelInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('ExcelInterface')
        self.stackWidget = QStackedWidget(self)

        self.initUI()
        self.initLayout()
        self.initButtonEvent()

    def initUI(self):
        self.homeFrame = ExcelHomeFrame()
        self.randomFrame = RandomExcelFrame()
        self.mergeView = MergeExcelView()
        self.toPdfView = ExcelToPdfView()
        self.sheetView = SheetToExcelView()
        self.keyMergeView = UnionQueryView()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.randomFrame)
        self.stackWidget.addWidget(self.mergeView)
        self.stackWidget.addWidget(self.toPdfView)
        self.stackWidget.addWidget(self.sheetView)
        self.stackWidget.addWidget(self.keyMergeView)

        self.stackWidget.setCurrentIndex(0)

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 40, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def initButtonEvent(self):
        self.homeFrame.randomExcel.clicked.connect(lambda: self.switchTo(self.randomFrame))
        self.homeFrame.to_pdf.clicked.connect(lambda: self.switchTo(self.toPdfView))
        self.homeFrame.merge_excel.clicked.connect(lambda: self.switchTo(self.mergeView))
        self.homeFrame.sheet_excel.clicked.connect(lambda: self.switchTo(self.sheetView))
        self.homeFrame.key_merge_excel.clicked.connect(lambda: self.switchTo(self.keyMergeView))

        self.randomFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.mergeView.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.toPdfView.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.sheetView.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.keyMergeView.returned.connect(lambda: self.switchTo(self.homeFrame))

    def switchTo(self, widget):
        self.stackWidget.setCurrentWidget(widget)
